package main

import (
	"fmt"
	"i18n-template/components"
	"i18n-template/router"
	"i18n-template/table"
	"net/http"
	"os"

	"github.com/gin-contrib/gzip"
	"github.com/gin-gonic/gin"
)

func main() {
	//components.InitRedis()
	db, err := components.InitGormController()
	if err != nil {
		fmt.Println("=> 链接数据库失败 ", err.Error())
		return
	}
	err = db.AutoMigrate(table.Users{},
		table.Article{}, table.File{},
		table.Inductance{}, table.Photoelectric{},
		table.Capacitance{}, table.Measurement{},
		table.Index{},
	)
	if err != nil {
		fmt.Println("=> 创建表失败 ", err.Error())
		return
	}
	r := router.InitRouter()
	r.GET("/upload/*filepath", func(context *gin.Context) {
		filepath := context.Param("filepath")
		downUrl := context.DefaultQuery("down", "")
		if downUrl != "" {
			context.Writer.Header().Set("Content-Disposition", "attachment; filename=\""+filepath+"\"")
		}
		//dir, _ := os.Getwd()
		file := "upload" + filepath
		if _, err := os.Stat(file); os.IsNotExist(err) {
			context.String(http.StatusNotFound, "File not found  "+err.Error())
			return
		}
		context.File(file)
	})
	r.Use(gzip.Gzip(gzip.DefaultCompression))
	r.Run(":60012")
}
